package com.ruoyi.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.domain.Emoticon;
import com.ruoyi.business.domain.bo.EmoticonBo;
import com.ruoyi.business.domain.vo.EmoticonVo;
import com.ruoyi.business.mapper.EmoticonMapper;
import com.ruoyi.business.service.IEmoticonService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysOss;
import com.ruoyi.system.service.ISysOssService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 最爱喝兽奶
 * @create 2022/7/19
 **/
@Service
public class EmoticonServiceImpl extends ServiceImpl<EmoticonMapper, Emoticon> implements IEmoticonService {

    private final Logger logger = LoggerFactory.getLogger(EmoticonServiceImpl.class);

    @Autowired
    private ISysOssService ossService;

    @Transactional
    @Override
    public Boolean insertEmoticon(List<EmoticonBo> list) throws IOException {
        int len = 0;
        for (EmoticonBo bo : list) {
//            if (!bo.getName().equals("[doge]")) continue;
            if (bo.getBilibiliUrl() != null) {
                URL url = new URL(bo.getBilibiliUrl());
                try {
                    URLConnection urlConnection = url.openConnection();
                    String suffix = bo.getBilibiliUrl().substring(bo.getBilibiliUrl().lastIndexOf('.'));
//                    FileUtils.writeToLocal(url.openConnection().getInputStream(), "D:\\emoji", bo.getName() + suffix);
                    SysOss sysOss = ossService.upload(urlConnection.getInputStream(), bo.getName(), suffix);
                    urlConnection.getInputStream().close();
                    Emoticon emoticon = new Emoticon();
                    BeanUtils.copyProperties(bo, emoticon);
                    emoticon.setId(new Date().getTime());
                    emoticon.setImgUrl(sysOss.getUrl());
                    baseMapper.insert(emoticon);
                    len++;
                }catch (Exception e) {
                    if (logger.isWarnEnabled()) {
                        logger.warn("上传" + bo.getName() + "表情包失败");
                    }
                }
            }else {
                Emoticon emoticon = new Emoticon();
                BeanUtils.copyProperties(bo, emoticon);
                baseMapper.insert(emoticon);
                len++;
            }
        }
        return len == list.size();
    }

    @Override
    public List<EmoticonVo> selectList(EmoticonBo bo) {
        List<Emoticon> emoticons = baseMapper.selectList(buildQueryWrapper(bo));
        List<EmoticonVo> list = emoticons.stream().map(item -> {
            EmoticonVo vo = new EmoticonVo();
            BeanUtils.copyProperties(item, vo);
            return vo;
        }).collect(Collectors.toList());
        return list;
    }

    private LambdaQueryWrapper<Emoticon> buildQueryWrapper(EmoticonBo bo) {
        LambdaQueryWrapper<Emoticon> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNotEmpty(bo.getType()), Emoticon::getType, bo.getType());
        lqw.eq(StringUtils.isNotEmpty(bo.getTypeName()), Emoticon::getTypeName, bo.getTypeName());
        return lqw;
    }
}
